//
//  YHDiscoveryTableView.m
//  FAKER
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 YHLai. All rights reserved.
//

#import "YHDiscoveryTableView.h"
#import "AllHeader.h"
#import "FitHeader.h"

#define RECOMMEND_URLSTRING @"http://dis.myzaker.com/api/list_discussion.php?_appid=iphone&except_subscribe=N"
#define OTHER_URLSTRING @"http://dis.myzaker.com/api/list_discussion.php?_appid=iphone&act=more_discussion&except_recommend=Y"

#define URLSTRING @"http://dis.myzaker.com/api/list_discussion.php?_appid=iphone&act=more_discussion"

@implementation YHDiscoveryTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //获取数据
        _dataSourceArray = [NSMutableArray array];
        [self getData];
        
        //创建精选tableView
        _tableView = [[BaseClassTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.HEIGHT) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        [self addSubview:_tableView];
        
        //下拉刷新
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            _flag = 0;
            [self getData];
            [_tableView.mj_footer resetNoMoreData];
        }];
        //上拉加载
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            _flag = 1;
            [self getData];
//            [_tableView.mj_footer endRefreshingWithNoMoreData];
        }];
    }
    return self;
}

#pragma mark - 获取数据
- (void)getData
{
    //判断是刷新还是加载
    _urlString = RECOMMEND_URLSTRING;
    if (_flag) {
        _urlString = OTHER_URLSTRING;
    }
    [InternetDataRequest getDataByString:_urlString BodyDic:nil WithDataBlock:^(id data) {
        [_tableView.mj_header endRefreshing];
        if (!_flag) {
            [_dataSourceArray removeAllObjects];
        } else {
            [_tableView.mj_footer endRefreshingWithNoMoreData];
        }
        NSArray *listArray = [[(NSDictionary *)data objectForKey:@"data"] objectForKey:@"list"];
        for (NSDictionary *dict in listArray) {
            //删除ZAKER相关
            if ([[dict objectForKey:@"title"] isEqualToString:@"ZAKER话题建议与反馈"]) {
                continue;
            }
            
            YHDiscovery *discovery = [[YHDiscovery alloc] init];
            [discovery setValuesForKeysWithDictionary:dict];
            [_dataSourceArray addObject:discovery];
        }
        [_tableView reloadData];
    }];
}

#pragma mark - tableView
#pragma mark cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}
#pragma mark cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"discovery";
    YHDiscoveryTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[YHDiscoveryTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    cell.delegate = self;
    cell.discovery = _dataSourceArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70 * FITHEIGHT;
}
#pragma mark cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    YHTopicViewController *topicVC = [[YHTopicViewController alloc] init];
//    YHDiscovery *discovery = _dataSourceArray[indexPath.row];
//    topicVC.urlString = discovery.api_url;
//    topicVC.topicName = discovery.title;
//    topicVC.coverImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:discovery.pic]]];
//    
//    //推出topicVC
//    topicVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
//    [[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:topicVC animated:YES completion:nil];
    
    //实现协议方法进入topicVC
    [self.delegate yhClickEnterIntoTopicVC:_dataSourceArray[indexPath.row]];
}

#pragma makr - 协议方法刷新数据
- (void)yhReloadDataWithStatus:(BOOL)status
{
    [self.delegate yhShowAlertViewWithStatus:status];
    [_tableView reloadData];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
